import pymysql

if __name__ == '__main__':
    # 连接数据库
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='123456',
        database='thinkcmf'
    )
    cursor = connection.cursor()
    # 假设表名列表保存在一个名为'table_names.txt'的文件中，每行一个表名
    with open('table_names.txt', 'r') as file:
        table_names = file.readlines()
    new_column_name = "did"
    new_column_type = "int(11)"
    for table_name in table_names:
        table_name = table_name.strip()  # 去除换行符等
        sql = f"ALTER TABLE {table_name} ADD COLUMN {new_column_name} {new_column_type} DEFAULT 1 Comment'平台id'"
        # sql = f"ALTER TABLE {table_name} DROP COLUMN {new_column_name}"
        cursor.execute(sql)
    connection.commit()
    cursor.close()
    connection.close()